package com.wisnews.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wisnews.mapper.SqlExecuteDevice;
import com.wisnews.pojo.sr.entity.Hobby;
import com.wisnews.pojo.sr.entity.Member;
import com.wisnews.service.IWisdomPush;
import com.wisnews.service.sr.IHobbyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Description：
 * @Author: yuyd
 * @Email: yyd8358@foxmail.com
 * @Date: 2021/2/23 16:33
 * @Version 1.0
 */
@Service
@Slf4j
public class WisdomPushImpl implements IWisdomPush {

    @Autowired
    private IHobbyService hobbyService;

    @Autowired
    private SqlExecuteDevice sqlExecuteDevice;

    @Override
    public void pushViewGenerateDevice(Member member) {
    //创建或重置覆盖视图
        StringBuilder stringBuffer = new StringBuilder(" CREATE OR REPLACE VIEW ");
        stringBuffer.append("view_"+member.getUserId()).append("  AS SELECT * FROM `t_sr_news` a ");
        String userHobbys = member.getUserHobbys();
        String[] hobby = userHobbys.split(",");
        List<String> list = Arrays.asList(hobby);
        if (Objects.isNull(list)){
            return;
        }

        stringBuffer.append(" where ");

        list.forEach(love -> {
            if (StringUtils.isNotBlank(love)){
                Hobby oneHobby = hobbyService.getById(Integer.parseInt(love));
                if (!Objects.isNull(oneHobby)){
                    stringBuffer.append(" a.`news_type` LIKE '%").append(oneHobby.getHobbyName()).append("%' or");
                }
            }
        });
        String mysqlStr = stringBuffer.toString();
        int lastOrIndex = mysqlStr.lastIndexOf("or");
        //没有爱好
        if (lastOrIndex == -1){
            return;
        }
        String substring = mysqlStr.substring(0, lastOrIndex);

        try {

            sqlExecuteDevice.sqlExecuteDevice(substring);
        }catch (SQLException e){
            log.error(e.toString());
        }
    }

    @Override
    public IPage pushDevice(Member member,IPage iPage) throws SQLException{

        if (ObjectUtils.isNull(member)){
            throw new IllegalStateException("访问用户数据为空！");
        }

        String viewName = "view_"+member.getUserId();
        pushViewGenerateDevice(member);
        IPage page = sqlExecuteDevice.seleteViewDate(viewName, iPage);

        return page;
    }

    @Override
    public IPage pushImportantNews(IPage iPage) throws SQLException{
        //先更新视图再查询
        String sql = " CREATE OR REPLACE VIEW view_guojiadashi AS SELECT * FROM  `t_sr_news` a where a.`news_type` LIKE '%新闻中心%' or a.`news_type` LIKE '%网易科技%'";
        sqlExecuteDevice.sqlExecuteDevice(sql);
        return sqlExecuteDevice.seleteViewDate("view_guojiadashi", iPage);

    }
}
